<?php
namespace Modules\Activity\Http\Controllers\Client\RotateLottery;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\RotateLottery\Activity;

use \Modules\Activity\Entities\Ticket;
// use \Modules\Agent\Entities\InviteRecord;
// use \Modules\Activity\Entities\ActivityRecord;

use Validator;
use ThrowException;
use DB;
use Auth;

class ActivityController extends Controller
{
    public function index(Request $req)
    {
        $options = $req->all();
        $options['per_page'] = 200;
        $options['scopes'] = ['onStock'];
        $options['only'] = [
            'id',
            'uuid',
            'money_price',
            'score_price',
            'title',
            'thumb',
            'activity_type_text',
            'activity_type'
        ];

        $list = Activity::getList($options);

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        // 
        $item = Activity::getEntity([
            'uuid' => $uuid,
            'with' => ['skus']
        ]);

        $only = [
            'id',
            'uuid',
            'title',
            'bg',
            'outer_bg',
            'center_bg',
            'score_bg',
            'detail_images',
            'end_at',
            'start_at',
            'image_3d',
            'images',
            'money_price',
            'score_price',
            'sub_title',
            'thumb',
            'theme',
            'union_status',
            'union_status_text',
            'visit_total',
            'is_invite_enabled',
            'skus' => [
                '*' => [
                    'id',
                    'uuid',
                    'title',
                    'thumb',
                    'money_price',
                    'score_price'
                    // 'stock',
                ]
            ]
        ];

        $info = $item->getInfo($only);

        $info['rule_list'] = explode("\n", $item->rule);

        $info['my_ticket_total'] = Ticket::getTotal(Auth::user(), $item);

        // 邀请统计
        $info['invite_info'] = $item->calcInviteInfo(Auth::user());

        $item->addVisit(1);

        return [
            'info' => $info
        ];
    }
}
